﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;
using System.Collections.Generic;

public partial class admin_EditPopedomGroup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 51)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if(!IsPostBack)
        {
            PopedomGroup pg = PopedomGroupManager.FindByPopedomGroupId(Convert.ToInt32(Request.QueryString["pid"]));
            if (pg.PopedomList!=null)
            {
                foreach (Popedom var in pg.PopedomList)
                {
                    ListBox2.Items.Add(new ListItem(var.PopedomName, var.PopedomId.ToString()));
                }
            }
            
            foreach (Popedom var in PopedomManager.FindAll())
            {
                bool flag = false;
                if (pg.PopedomList != null)
                {
                    foreach (Popedom item in pg.PopedomList)
                    {
                        if (item.PopedomId == var.PopedomId && item.PopedomName.Equals(var.PopedomName))
                        {
                            flag = true;
                        }
                    }
                }
                if(!flag)
                {
                    ListBox1.Items.Add(new ListItem(var.PopedomName,var.PopedomId.ToString()));
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string popedomList = "";
        foreach (ListItem var in ListBox2.Items)
        {
            popedomList += var.Value+" ";
        }
        //Label3.Text = popedomList;
        PopedomGroup pg = new PopedomGroup();
        pg.PopedomGroupId = Convert.ToInt32(Request.QueryString["pid"]);
        pg.PopedomListString = popedomList.Trim();
        PopedomGroupManager.UpdatePopedomGroupList(pg);
        Response.Redirect("PopedomGroupList.aspx");
    }
    //protected void Button3_ServerClick(object sender, EventArgs e)
    //{
    //    ArrayList arrayValue = new ArrayList();
    //    for (int i = 0; i < ListBox1.Items.Count; i++)
    //    {
    //        if (ListBox1.Items[i].Selected)
    //        {
    //            ListBox2.Items.Add(new ListItem(ListBox1.Items[i].Text, ListBox1.Items[i].Value));
    //            arrayValue.Add(ListBox1.Items[i].Value);
    //        }
    //    }
    //    for (int i = 0; i < arrayValue.Count; i++)
    //    {
    //        for (int j = 0; j < ListBox1.Items.Count; j++)
    //        {
    //            if (ListBox1.Items[j].Value == arrayValue[i].ToString())
    //            {
    //                ListBox1.Items.RemoveAt(j);
    //            }
    //        }
    //    }
    //}
    //protected void Button4_ServerClick(object sender, EventArgs e)
    //{
    //    ArrayList arrayValue = new ArrayList();
    //    for (int i = 0; i < ListBox2.Items.Count; i++)
    //    {
    //        if (ListBox2.Items[i].Selected)
    //        {
    //            ListBox1.Items.Add(new ListItem(ListBox2.Items[i].Text, ListBox2.Items[i].Value));
    //            arrayValue.Add(ListBox2.Items[i].Value);
    //        }
    //    }
    //    for (int i = 0; i < arrayValue.Count; i++)
    //    {
    //        for (int j = 0; j < ListBox2.Items.Count; j++)
    //        {
    //            if (ListBox2.Items[j].Value == arrayValue[i].ToString())
    //            {
    //                ListBox2.Items.RemoveAt(j);
    //            }
    //        }
    //    }
    //}
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList arrayValue = new ArrayList();
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            if (ListBox1.Items[i].Selected)
            {
                ListBox2.Items.Add(new ListItem(ListBox1.Items[i].Text, ListBox1.Items[i].Value));
                arrayValue.Add(ListBox1.Items[i].Value);
            }
        }
        for (int i = 0; i < arrayValue.Count; i++)
        {
            for (int j = 0; j < ListBox1.Items.Count; j++)
            {
                if (ListBox1.Items[j].Value == arrayValue[i].ToString())
                {
                    ListBox1.Items.RemoveAt(j);
                }
            }
        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList arrayValue = new ArrayList();
        for (int i = 0; i < ListBox2.Items.Count; i++)
        {
            if (ListBox2.Items[i].Selected)
            {
                ListBox1.Items.Add(new ListItem(ListBox2.Items[i].Text, ListBox2.Items[i].Value));
                arrayValue.Add(ListBox2.Items[i].Value);
            }
        }
        for (int i = 0; i < arrayValue.Count; i++)
        {
            for (int j = 0; j < ListBox2.Items.Count; j++)
            {
                if (ListBox2.Items[j].Value == arrayValue[i].ToString())
                {
                    ListBox2.Items.RemoveAt(j);
                }
            }
        }
    }
}
